#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include "smpeg.h"
#include "perso.h"

int choisir_perso (SDL_Surface *ecran, SDL_Event event, int *p, int *maxW, int *maxH, int *boucle_prin)
{
	int continuer =1;
	static int i=0;
	static int active=0;
    static int controle=0;
    
    
	SDL_Surface *perso1 = NULL, *perso2 = NULL, *perso3 = NULL , *perso4 = NULL, *perso5 = NULL, *next = NULL, *back = NULL, *nextClic = NULL, *backClic = NULL;
	SDL_Surface *perso1Clic = NULL, *perso2Clic = NULL, *perso3Clic = NULL, *perso4Clic = NULL, *perso5Clic = NULL, *titre = NULL;

	SDL_Rect pos_perso1, pos_perso2, pos_perso3, pos_perso4, pos_perso5, pos_next, pos_back, pos_titre;

	pos_perso3.x = (*maxW-168)/2;
	pos_perso3.y = *maxH/3.18;
	
	pos_perso2.x = pos_perso3.x - 168;
	pos_perso2.y = *maxH/3.18;
	
	pos_perso1.x = pos_perso2.x - 168;
	pos_perso1.y = *maxH/3.18;
	
	pos_perso4.x = pos_perso3.x + 168;
	pos_perso4.y = *maxH/3.18;
	
	pos_perso5.x = pos_perso4.x + 168;
	pos_perso5.y = *maxH/3.18;

	pos_next.x = *maxW - 370;
	pos_next.y = *maxH /1.1666;

	pos_back.x = 10;
	pos_back.y = *maxH /1.1666;

    pos_titre.x = (*maxW - 350)/2;
    pos_titre.y = *maxH /4.375;


	perso1 = IMG_Load ("menu/choisir/hadi.png");
	perso2 = IMG_Load ("menu/choisir/habib.png");
	perso3 = IMG_Load  ("menu/choisir/souha.png");
	perso4 = IMG_Load ("menu/choisir/selim.png"); 
	perso5 = IMG_Load ("menu/choisir/mohsen.png");

	perso1Clic = IMG_Load ("menu/choisir/hediClic.png");
	perso2Clic = IMG_Load ("menu/choisir/HabibClic.png");
	perso3Clic = IMG_Load ("menu/choisir/souhaClic.png");
	perso4Clic = IMG_Load ("menu/choisir/SelimClic.png");
	perso5Clic = IMG_Load ("menu/choisir/mohsenClic.png");

	next = IMG_Load("menu/choisir/select.png");
	nextClic = IMG_Load("menu/choisir/selectClic.png");
	back = IMG_Load("menu/choisir/back.png");
	backClic = IMG_Load("menu/choisir/backClic.png");
    titre = IMG_Load("menu/select.png");

	

    
            switch (event.type)
            {
            	case SDL_QUIT:
                    return EXIT_SUCCESS;
                    break;

            	
            	case SDL_MOUSEMOTION:
            	if (event.motion.x>= pos_perso1.x && event.motion.x<= pos_perso1.x+165 && event.motion.y>= pos_perso1.y && event.motion.y<= pos_perso1.y+320 && controle==0 )
            	{
            		i=1;
            	}
            	else if (event.motion.x>= pos_perso2.x && event.motion.x<= pos_perso2.x+165 && event.motion.y>= pos_perso2.y && event.motion.y<= pos_perso2.y+320 && controle==0 )
            	{
            		i=2;
            	}
            	else if (event.motion.x>= pos_perso3.x && event.motion.x<= pos_perso3.x+165 && event.motion.y>= pos_perso3.y && event.motion.y<= pos_perso3.y+320 && controle==0 )
            	{
            		i=3;
            	}
            	else if (event.motion.x>= pos_perso4.x && event.motion.x<= pos_perso4.x+165 && event.motion.y>= pos_perso4.y && event.motion.y<= pos_perso4.y+320 && controle==0 )
            	{
            		i=4;
            	}
            	else if (event.motion.x>= pos_perso5.x && event.motion.x<= pos_perso5.x+165 && event.motion.y>= pos_perso5.y && event.motion.y<= pos_perso5.y+320 && controle==0 ) 
            	{
            		i=5;
            	}
                else if (event.motion.x>= pos_back.x && event.motion.x<= pos_back.x+330 && event.motion.y>= pos_back.y && event.motion.y<= pos_back.y+35 && controle==0 )

                {
                    i=6;
                }
                else if (event.motion.x>= pos_next.x && event.motion.x<= pos_next.x+330 && event.motion.y>= pos_next.y && event.motion.y<= pos_next.y+35 && active !=0 && controle==0 ) 
                {
                    i=7;
                }
            	else if (controle==0)
            	{
            		i=0;
            	}

            	break;
            	

            	case SDL_MOUSEBUTTONUP:
                {
                    if (event.button.x>= pos_perso1.x && event.button.x<= pos_perso1.x+165 && event.button.y>= pos_perso1.y && event.button.y<= pos_perso1.y+320  && controle==0 )
                    {
                        i=1;
                        active=1;
                        
                    }
                    else if (event.motion.x>= pos_perso2.x && event.motion.x<= pos_perso2.x+165 && event.motion.y>= pos_perso2.y && event.motion.y<= pos_perso2.y+320  && controle==0 )
                    {
                        
                        i=2;
                        active=2;
                        
                    }
                    else if (event.motion.x>= pos_perso3.x && event.motion.x<= pos_perso3.x+165 && event.motion.y>= pos_perso3.y && event.motion.y<= pos_perso3.y+320  && controle==0 )
                    {
                        
                        i=3;
                        active=3;
                        
                    }
                    else if (event.motion.x>= pos_perso4.x && event.motion.x<= pos_perso4.x+165 && event.motion.y>= pos_perso4.y && event.motion.y<= pos_perso4.y+320&& controle==0 )
                    {
                        
                        i=4;
                        active=4;
                    }
                    else if (event.motion.x>= pos_perso5.x && event.motion.x<= pos_perso5.x+165 && event.motion.y>= pos_perso5.y && event.motion.y<= pos_perso5.y+320  && controle==0 )
                    {
                        
                        i=5;
                        active=5;
                        
                    }
                    else if (event.motion.x>= pos_back.x && event.motion.x<= pos_back.x+330 && event.motion.y>= pos_back.y && event.motion.y<= pos_back.y+35 && controle==0 )
                    {

                        *p=0;
                    }
                    else if (event.motion.x>= pos_next.x && event.motion.x<= pos_next.x+330 && event.motion.y>= pos_next.y && event.motion.y<= pos_next.y+35 && active !=0) 
                    {
                        i=8;
                    }
                    else if (controle==1)
                    {
                        i=8;
                    }
                }
                break;
            }

        
            perso(&active);
	
    	if (i==1)
            {
            	if (active==2)
            	{
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            	}
            	if (active==3)
            	{
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            	}
            	if (active==4)
            	{
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            	}
            	if (active==5)
            	{
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            	}
            	SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            	SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
				SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
				SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
				SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
                SDL_BlitSurface(titre, NULL, ecran, &pos_titre);
            		
            }
        if (i==2)
            {
            	if (active==1)
            	{
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            	}
            	if (active==3)
            	{
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            	}
            	if (active==4)
            	{
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            	}
            	if (active==5)
            	{
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            	}
            	SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
            	SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
				SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
				SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
				SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
                SDL_BlitSurface(titre, NULL, ecran, &pos_titre);
            		
            }
        if (i==3)
            {
            	if (active==1)
            	{
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            	}
            	if (active==2)
            	{
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            	}
            	if (active==4)
            	{
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            	}
            	if (active==5)
            	{
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            	}
            	SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
				SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
            	SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            	SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
				SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
                SDL_BlitSurface(titre, NULL, ecran, &pos_titre);
            		
            }
        if (i==4)
            {
            	if (active==1)
            	{
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            	}
            	if (active==2)
            	{
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            	}
            	if (active==3)
            	{
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            	}
            	if (active==5)
            	{
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            	}
            	SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
				SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
				SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
            	SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            	SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
            	SDL_BlitSurface(titre, NULL, ecran, &pos_titre);	
            }
        if (i==5)
            {
            	if (active==1)
            	{
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            	}
            	if (active==2)
            	{
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            	}
            	if (active==3)
            	{
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            	}
            	if (active==4)
            	{
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            	}
            	SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
            	SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
				SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
				SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
            	SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
            	SDL_BlitSurface(titre, NULL, ecran, &pos_titre);	
            }
            if (i==0)
			{
				if (active==1)
            	{
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            		SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
            	}
            	if (active==2)
            	{
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            		SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
            	}
            	if (active==3)
            	{
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            		SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
            	}
            	if (active==4)
            	{
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            		SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
            	}
            	if (active==5)
            	{
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            		SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
            	}
				SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
				SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
				SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
				SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
				SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
    			SDL_BlitSurface(titre, NULL, ecran, &pos_titre);

			 }
            if (i==6)
            {
                SDL_BlitSurface(backClic, NULL, ecran, &pos_back);
                if (active==1)
                {
                    SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
                    SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
                }
                if (active==2)
                {
                    SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
                    SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
                }
                if (active==3)
                {
                    SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
                    SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
                }
                if (active==4)
                {
                    SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
                    SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
                }
                if (active==5)
                {
                    SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
                    SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
                }
                SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
                SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
                SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
                SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
                SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(next, NULL , ecran, &pos_next);
                SDL_BlitSurface(titre, NULL, ecran, &pos_titre);
            }
            if (i==7)
            {
                SDL_BlitSurface(nextClic, NULL, ecran, &pos_next);
                if (active==1)
                {
                    SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
                    SDL_BlitSurface(perso1Clic, NULL, ecran, &pos_perso1);
                }
                if (active==2)
                {
                    SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
                    SDL_BlitSurface(perso2Clic, NULL, ecran, &pos_perso2);
                }
                if (active==3)
                {
                    SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
                    SDL_BlitSurface(perso3Clic, NULL, ecran, &pos_perso3);
                }
                if (active==4)
                {
                    SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
                    SDL_BlitSurface(perso4Clic, NULL, ecran, &pos_perso4);
                }
                if (active==5)
                {
                    SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
                    SDL_BlitSurface(perso5Clic, NULL, ecran, &pos_perso5);
                }
                SDL_BlitSurface(perso1, NULL, ecran, &pos_perso1);
                SDL_BlitSurface(perso2, NULL, ecran, &pos_perso2);
                SDL_BlitSurface(perso3, NULL, ecran, &pos_perso3);
                SDL_BlitSurface(perso4, NULL, ecran, &pos_perso4);
                SDL_BlitSurface(perso5, NULL, ecran, &pos_perso5);
                SDL_BlitSurface(back, NULL , ecran, &pos_back);
                SDL_BlitSurface(titre, NULL, ecran, &pos_titre);
            }
            if (i==8)
            {
                controle=1;
                *boucle_prin= saisie(ecran, event,&controle, maxW, maxH, &boucle_prin);
                
            }


        SDL_Flip(ecran);

    
SDL_FreeSurface(perso1);
SDL_FreeSurface(perso2);
SDL_FreeSurface(perso3);
SDL_FreeSurface(perso4);
SDL_FreeSurface(perso5);
SDL_FreeSurface(perso1Clic);
SDL_FreeSurface(perso2Clic);
SDL_FreeSurface(perso3Clic);
SDL_FreeSurface(perso4Clic);
SDL_FreeSurface(perso5Clic);
SDL_FreeSurface(next);
SDL_FreeSurface(nextClic);
SDL_FreeSurface(back);
SDL_FreeSurface(backClic);
SDL_FreeSurface(titre);
}